﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IndignaFramework.Models;
using System.Web.Security;
using IndignaFramework.Controllers.Code;

namespace IndignaFramework.Controllers
{
    public class BackofficeController : Controller
    {
        private Model1Container db = new Model1Container();
        // GET: /Backoffice/

        [BackofficeLoginFilter]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login() {

            return View();
        
        }

        public ActionResult NotLoggedIn() {
            if (BackofficeMembership.isAuthenticated()) {
                Response.Redirect("/Backoffice/Index");
            }
            return View();
        
        }

        [HttpPost]
        public ActionResult Login(LogInModel model)
        {
            if (ModelState.IsValid)
            {
                var movimiento = MovimientoHelper.MovimientoId;
                var usuarios = db.Usuario.OfType<AdministradorWeb>().Where(u => u.Movimiento.id == movimiento);
                var usr = from u in usuarios
                          where u.Email == model.UserName
                          select u;
                if (usr.Count() == 1 && usr.First().Password == model.Password)
                {
                    var cookie = new HttpCookie("Backoffice");
                    HttpContext.Response.Cookies.Set(cookie);

                    Response.Redirect("/Backoffice/Index");
                    
                }
                else
                {
                    ModelState.AddModelError("", "El email o password no son correctos.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        [BackofficeLoginFilter]
        public ActionResult Logout() {

            var cookie = Request.Cookies.Get("Backoffice");
            cookie.Expires = DateTime.Now.AddDays(-10);
            Response.Cookies.Set(cookie);

            Response.Redirect("/Backoffice/Login");

            return View();
        }

        [BackofficeLoginFilter]
        public ActionResult GestionarUsuarios()
        {

            int movId = MovimientoHelper.MovimientoId;
            var usuarios = db.Usuario.OfType<UsuarioWeb>().Where(x => x.Contenido.Where(y => y.Inadecuado > 0).Count() > 0 && x.Activo == true
                            && x.Movimiento.id == movId);
            
            var users = from usr in usuarios
                        select new UsuarioAGestionar
                        {

                            Nombre = usr.Nombre + " " + usr.Apellido,
                            Email = usr.Email,
                            id = usr.id,
                            CantInadecuados = usr.Contenido.Where(x => x.Inadecuado > 0).Count()

                        };

            return View(users);

        }

        
    }
}
